
package com.bangcommunity.bbframe.springredis;

import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by tanghc on 17/1/13.
 *
 * 获得项目相关的redis客户端。 封装QtProjectRedisClient.getSpringRedisCache(),获取最新的客户端连接
 *
 */
public abstract class RedisClient {

    public static <T> RedisTemplate<String, T> getRedisTemplate() {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.getRedisTemplate();
    }

    public static void del(final String key) {
        ProjectRedisClient.getSpringRedisCache().del(key);
    }

    public static void del(final Collection<String> keys) {
        ProjectRedisClient.getSpringRedisCache().del(keys);
    }
    public static void delPattern(final String pattern) {
        Set<String> keys = ProjectRedisClient.getSpringRedisCache().keys(pattern);
        del(keys);
    }

    public static Boolean exists(final String key) {
        return ProjectRedisClient.getSpringRedisCache().exists(key);
    }

    public static Boolean expire(final String key, final long timeout, final TimeUnit unit) {
        return ProjectRedisClient.getSpringRedisCache().expire(key, timeout, unit);
    }

    public static void expireAt(final String key, final Date date) {
        ProjectRedisClient.getSpringRedisCache().expireAt(key, date);
    }

    public static Set<String> keys(final String pattern) {
        return ProjectRedisClient.getSpringRedisCache().keys(pattern);
    }

    public static String type(final String key) {
        return ProjectRedisClient.getSpringRedisCache().type(key);
    }

    public static <T> T get(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.get(key);
    }

    public static <T> T getSet(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.getSet(key, value);
    }

    public static Long incr(final String key, final long delta) {
        return ProjectRedisClient.getSpringRedisCache().incr(key, delta);
    }

    public static Long getIncr(final String key) {
        return ProjectRedisClient.getSpringRedisCache().getIncr(key);
    }
    public static String getString(final String key) {
        return ProjectRedisClient.getSpringRedisCache().getString(key);
    }

    public static <T> void set(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.set(key, value);
    }

    public static <T> void set(final String key, final T value, final long timeout, final TimeUnit unit) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.set(key, value, timeout, unit);
    }
    public static void setString(final String key, final String value) {
        ISpringRedisCache  springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.setString(key, value);
    }

    public static  void setString(final String key, final String value, final long timeout, final TimeUnit unit) {
        ISpringRedisCache springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.setString(key, value, timeout, unit);
    }
    public static void setIncr(final String key, final Long value) {
        ISpringRedisCache springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.setIncr(key, value);
    }

    public static void setIncr(final String key, final Long value, final long timeout, final TimeUnit unit) {
        ISpringRedisCache springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.setIncr(key, value, timeout, unit);
    }

    public static <T> boolean setNX(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.setNX(key, value);
    }

    // Hash

    public static void hDel(final String key, final Object... hKeys) {
        ProjectRedisClient.getSpringRedisCache().hDel(key, hKeys);
    }

    public static Boolean hExists(final String key, final String hKeys) {
        return ProjectRedisClient.getSpringRedisCache().hExists(key, hKeys);
    }

    public static <T> Map<String, T> hGet(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.hGet(key);
    }

    public static <T> T hGet(final String key, final String hKey) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.hGet(key, hKey);
    }

    public static Set<String> hKeys(final String key) {
        return ProjectRedisClient.getSpringRedisCache().hKeys(key);
    }

    public static Long hLen(final String key) {
        return ProjectRedisClient.getSpringRedisCache().hLen(key);
    }

    public static <T> void hSet(final String key, final String hk, final T hv) {
        ProjectRedisClient.getSpringRedisCache().hSet(key, hk, hv);
    }

    public static <T> void hSet(final String key, final Map<String, T> map) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.hSet(key, map);
    }

    public static <T> List<T> hVals(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.hVals(key);
    }

    // List

    public static <T> T lIndex(final String key, final long index) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lIndex(key, index);
    }

    public static <T> void lInsert(final String key, final long index, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.lInsert(key, index, value);
    }

    public static Long lLen(final String key) {
        return ProjectRedisClient.getSpringRedisCache().lLen(key);
    }

    public static <T> T lPop(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lPop(key);
    }

    public static <T> T lPop(final String key, final long timeout, final TimeUnit unit) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lPop(key, timeout, unit);
    }

    public static <T> Long lPush(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lPush(key, value);
    }
    public static <T> Long lPushAll(final String key, final T... values) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lPushAll(key, values);
    }

    public static <T> List<T> lRange(final String key, final long start, final long end) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lRange(key, start, end);
    }

    public static <T> Long lRem(final String key, final long index, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.lRem(key, index, value);
    }

    public static <T> void lSet(final String key, final long index, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.lSet(key, index, value);
    }

    public static void ltrim(final String key, final long start, final long end) {
        ProjectRedisClient.getSpringRedisCache().ltrim(key, start, end);
    }

    public static <T> Long rPush(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.rPush(key, value);
    }


    public static <T> Long rPushAll(final String key, final T... values) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.rPushAll(key, values);
    }

    public static <T> T rPop(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.rPop(key);
    }

    // Set

    public static <T> Long sAdd(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.sAdd(key, value);
    }

    public static <T> Set<T> sDiff(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.sDiff(key);
    }

    public static <T> Set<T> sMembers(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.sMembers(key);
    }

    public static <T> Boolean sIsMember(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.sIsMember(key, value);
    }

    public static <T> T sPop(final String key) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.sPop(key);
    }

    public static <T> Long sRem(final String key, final T value) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.sRem(key, value);
    }

    public static Long sCard(String key) {
        return ProjectRedisClient.getSpringRedisCache().sCard(key);
    }

    // SortedSet

    public static <T> void zAdd(final String key, final T value, final double score) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        springRedisCache.zAdd(key, value, score);
    }

    public static <T> Set<T> zRange(final String key, final long start, final long end) {
        ISpringRedisCache<T> springRedisCache = ProjectRedisClient.getSpringRedisCache();
        return springRedisCache.zRange(key, start, end);
    }

    public static Long zRem(final String key, final Object... values) {

        return ProjectRedisClient.getSpringRedisCache().zRem(key, values);
    }

    public static Long zRemByRange(final String key, final long start, final long end) {

        return ProjectRedisClient.getSpringRedisCache().zRemByRange(key, start,end);
    }

    public static Long zRemByScore(final String key, double start, double end) {

        return ProjectRedisClient.getSpringRedisCache().zRemByScore(key, start, end);
    }

    public static Long zCard(String key) {
        return ProjectRedisClient.getSpringRedisCache().zCard(key);

    }

    public static <T> T execute(final RedisScript<T> script, final List<String> keys, final Object... args) {
        return ProjectRedisClient.getSpringRedisCache().execute(script, keys, args);

    }

    public static <T> T execute(final RedisScript<T> script, final RedisSerializer<?> argsSerializer,
            final RedisSerializer<T> resultSerializer, final List<String> keys, final Object... args) {
        return ProjectRedisClient.getSpringRedisCache().execute(script, argsSerializer, resultSerializer, keys, args);

    }

    public static <T> T execute(final RedisCallback<T> action) {
        return ProjectRedisClient.getSpringRedisCache().execute(action);

    }

    public static <T> T execute(final SessionCallback<T> session) {
        return ProjectRedisClient.getSpringRedisCache().execute(session);

    }

    public static String wrapPrefix(String key) {
        return ProjectRedisClient.getSpringRedisCache().wrapPrefix(key);
    }
}
